<?php
class Zendvn_System_Plugin extends Zend_Controller_Plugin_Abstract
{

    /**
     * Called before Zend_Controller_Front calls on the router
     * to evaluate the request against the registered routes.
     * @param Zend_Controller_Request_Abstract $request
     */
    public function routeStartup(Zend_Controller_Request_Abstract $request)
    {

    }

    /**
     * Called after the router finishes routing the request.
     * @param Zend_Controller_Request_Abstract $request
     */
    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
        
    }

    /**
     * Called before Zend_Controller_Front enters its dispatch loop.
     * @param Zend_Controller_Request_Abstract $request
     */
    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
    {
        //Get module,controller,action name
        $moduleName = $request->getModuleName();
        $moduleController = $request->getControllerName();
        $moduleAction = $request->getActionName();
        
        //Get cookie
        if (isset($_COOKIE['computer_user'])){
            // Decode cookie
            $detailUser = (array)json_decode($_COOKIE['computer_user']);
            // Write session
            $session = Core_Session::getInstance('user');
            $session->username  = $detailUser['username'];
            $session->first_name  = $detailUser['first_name'];
            $session->last_name  = $detailUser['last_name'];
            $session->full_name  = $detailUser['first_name'] .' '.$detailUser['last_name'];
            $session->avatar  = $detailUser['avatar'];
            $session->group  = $detailUser['group'];
        }
        
        //Check login
        $user_name = Core_Session::getInstance('user')->username;
        if($user_name == null && $moduleController != 'login'){
            $this->_response->setRedirect(BASE_URL.'/default/login')->sendResponse();
        }
        
        //Init layout
        Zend_Layout::startMvc(array(
            'layout'     => 'index',
            'layoutPath' => PUBLIC_PATH . DS . 'layout'
        ));
    }

    /**
     * Called before an action is dispatched by the dispatcher.
     * This callback allows for proxy or filter behavior.
     * By altering the request and resetting its dispatched flag
     * via Zend_Controller_Request_Abstract::setDispatched(false),
     * the current action may be skipped and/or replaced.
     * @param Zend_Controller_Request_Abstract $request
     */
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        
    }

    /**
     * Called after an action is dispatched by the dispatcher.
     * This callback allows for proxy or filter behavior.
     * By altering the request and resetting its dispatched flag
     * via Zend_Controller_Request_Abstract::setDispatched(false)),
     * a new action may be specified for dispatching.
     * @param Zend_Controller_Request_Abstract $request
     */
    public function postDispatch(Zend_Controller_Request_Abstract $request)
    {
        
    }

    /**
     * Called after Zend_Controller_Front exits its dispatch loop.
     */
    public function dispatchLoopShutdown()
    {
        
    }

}
?>